home *** CD-ROM | disk | FTP | other *** search
/ Nautilus 1992 July / Nautilus-3-8 / Nautilus-3-8.bin / Tools & Utilities / Techy Stuff / Source ƒ / egrep-1.5 / grep-src / FakeAlert.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-08-25  |  4.0 KB  |  157 lines

  1. #include    <Dialogs.h>
  2. #include <Memory.h>
  3. #include "FakeAlert.h"
  4.  
  5. static SetDControl(DialogPtr theDialog, Integer itemNo, StringPtr title, Boolean makeBold);
  6.  
  7. /*
  8.     In-memory item list for dialog with four items:
  9.  
  10.     1    "^0^1^2^3" (static text)
  11.     2    Button 1
  12.     3    Button 2
  13.     4    Button 3
  14.  
  15.     The caller of FakeAlert passes the four strings that are to be
  16.     substituted into the first item, the number of buttons that
  17.     should be used, and the titles to put into each button.
  18.     A copy of the item list is hacked to use the right number of
  19.     buttons.
  20.  
  21.     Thanks to Erik Kilk and Jason Haines.  Some of the stuff to do
  22.     this is modified from code they wrote.
  23. */
  24.  
  25.  
  26. static Integer    itemList [] =
  27. {
  28.     3,                    /* max number of items - 1 */
  29.  
  30. /*
  31.     statText item
  32. */
  33.     0, 0,                /* reserve a Longint for item handle */
  34.     10, 27, 61, 225,    /* display rectangle */
  35.     ((8+128) << 8) | 8,    /* 8 + 128 = statText (disabled), title 8 bytes long */
  36.     '^0', '^1',        /* ^0^1^2^3 */
  37.     '^2', '^3',
  38.  
  39. /*
  40.     first button
  41. */
  42.  
  43.     0, 0,                /* reserve a Longint for item handle */
  44.     104, 140, 124, 210,    /* display rectangle */
  45.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long*/
  46.  
  47. /*
  48.     second button
  49. */
  50.  
  51.     0, 0,                /* reserve a Longint for item handle */
  52.     104, 30, 124, 100,    /* display rectangle */
  53.     (4 << 8) | 0,        /* 4 = pushButton, title is 0 bytes long */
  54.  
  55. /*
  56.     third button
  57. */
  58.  
  59.     0, 0,                /* reserve a Longint for item handle */
  60.     72, 30, 92, 100,    /* display rectangle */
  61.     (4 << 8) | 0        /* 4 = pushButton, title is 0 bytes long */
  62. };
  63.  
  64.  
  65. /*
  66.     Set dialog button title and draw bold outline if makeBold true.
  67.     This must be done after the window is shown or else the bold
  68.     outline won't show up (which is probably the wrong way to do it).
  69. */
  70.  
  71. static SetDControl (DialogPtr theDialog, Integer itemNo, StringPtr title, Boolean makeBold)
  72. {
  73. Handle itemHandle;
  74. Integer        itemType;
  75. Rect        itemRect;
  76. PenState    pState;
  77.  
  78.     GetDItem (theDialog, itemNo, &itemType, &itemHandle, &itemRect);
  79.     SetCTitle ((ControlHandle) itemHandle, title);
  80.     if (makeBold)
  81.     {
  82.         GetPenState (&pState);
  83.         PenNormal ();
  84.         PenSize (3, 3);
  85.         InsetRect (&itemRect, -4, -4);
  86.         FrameRoundRect (&itemRect, 16, 16);
  87.         SetPenState (&pState);
  88.     }
  89. }
  90.  
  91.  
  92. /*
  93.     Fake an alert, using an in-memory window and item list.
  94.     The message to be presented is constructed from the first
  95.     four arguments.  nButtons is the number of buttons to use,
  96.     defButton is the default button, the next three args are
  97.     the titles to put into the buttons.  The return value is
  98.     the button number (1..nButtons).  This must be interpreted
  99.     by the caller, since the buttons may be given arbitrary
  100.     titles.
  101.  
  102.     nButtons should be between 1 and 3, inclusive.
  103.     defButton should be between 1 and nButtons, inclusive.
  104. */
  105.  
  106.  
  107. FakeAlert (StringPtr s1, StringPtr s2, StringPtr s3, StringPtr s4,
  108.           Integer nButtons, Integer defButton,
  109.           StringPtr t1, StringPtr t2, StringPtr t3)
  110. {
  111. GrafPtr        savePort;
  112. register DialogPtr    theDialog;
  113. register Handle        iListHandle;
  114. Rect        bounds;
  115. Integer        itemHit;
  116.  
  117.     InitCursor ();
  118.     GetPort (&savePort);
  119.     iListHandle = NewHandle ((Size)512);
  120.     HLock (iListHandle);
  121.     BlockMove (&itemList, *iListHandle, 512L);
  122.     ((Integer *) *iListHandle)[0] = nButtons;    /* = number items - 1 */
  123.     SetRect (&bounds, 115, 80, 355, 220);
  124.     theDialog = NewDialog (nil, &bounds, "\p", false, dBoxProc, 
  125.                            (WindowPtr) -1, false, 0L, iListHandle);
  126.  
  127.     ParamText (s1, s2, s3, s4);        /* construct message */
  128.  
  129.     SetPort (theDialog);
  130.     ShowWindow (theDialog);
  131.  
  132.     switch (nButtons)                /* set button titles */
  133.     {
  134.         case 3:
  135.             SetDControl (theDialog, 4, t3, defButton == 3);
  136.             /* fall through... */
  137.         case 2:
  138.             SetDControl (theDialog, 3, t2, defButton == 2);
  139.             /* fall through... */
  140.         case 1:
  141.             SetDControl (theDialog, 2, t1, defButton == 1);
  142.     }
  143.  
  144. /*
  145.     ModalDialog returns 1 if return/enter hit, which, since
  146.     the statText item is first, can be unambiguously
  147.     interpreted as "choose default".
  148. */
  149.     ModalDialog (nil, &itemHit);
  150.     itemHit = (itemHit == 1 ? defButton : itemHit - 1);
  151.     HUnlock (iListHandle);
  152.     /*HPurge (iListHandle);*/
  153.     DisposDialog (theDialog);
  154.     SetPort (savePort);
  155.     return (itemHit);
  156. }
  157.